package com.gimi.cloud.bbp.app.consumer.service.order.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.app.consumer.feign.OrderFeignService;
import com.gimi.cloud.bbp.app.consumer.service.order.OrderService;
import com.gimi.cloud.bbp.common.enums.CommonEnume;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.order.OrderDTO;

/**
 * 订单相关服务
 * @author 李林峰
 */
@Service
public class OrderServiceImpl implements OrderService {
	@Autowired OrderFeignService feignService;

	@Override
	public void userOrderList(String requestData, HttpServletRequest request, HttpServletResponse response) {
		OrderDTO parseObject = JSON.parseObject(requestData,OrderDTO.class);
        parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(feignService.userOrderList(parseObject), response);
	}

	@Override
	public void modifyOrderStatus(String requestData, HttpServletRequest request, HttpServletResponse response) {
		OrderDTO parseObject = JSON.parseObject(requestData,OrderDTO.class);
		String status = parseObject.getStatus();
		if (parseObject.getOId()==null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"订单ID不能为空");
		}
		if (StringUtils.isBlank(status)) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"订单状态不能为空");
		}
		if (!CommonEnume.OrderStatus.D01.name().equals(status)&&!CommonEnume.OrderStatus.A09.name().equals(status)) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"此接口只支持取消或删除订单");
		}
        parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(feignService.modifyOrderStatus(parseObject), response);
	}

	@Override
	public void getOrderDetails(String requestData, HttpServletRequest request, HttpServletResponse response) {
		OrderDTO parseObject = JSON.parseObject(requestData,OrderDTO.class);
		if (parseObject.getOId()==null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"订单ID不能为空");
		}
        parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(feignService.getOrderDetails(parseObject), response);
		
	}
}
